package com.egg.ylt.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.activity.ACT_ShopDetail;
import com.egg.ylt.pojo.NatatoriumListPageEntity;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ViewHolder;
import com.yonyou.framework.library.common.CommonUtils;

import java.util.List;

/**
 * Created by lijiateng on 2019/1/16.
 */

public class ADA_RecommendShop extends CommonAdapter<NatatoriumListPageEntity.ListBean> {

    public ADA_RecommendShop(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_recommended_shop;
    }

    @Override
    protected void convert(ViewHolder holder, final NatatoriumListPageEntity.ListBean item, int position) {

        LinearLayout root = (LinearLayout) holder.getConvertView();
        LinearLayout topLayout = root.findViewById(R.id.ll_top_layout);

        if (position == 3) {
            root.setBackgroundResource(R.drawable.shape_bg_shop_top_corner);
            topLayout.setVisibility(View.VISIBLE);
        } else if (position == mDatas.size() + 2) {
            root.setBackgroundResource(R.drawable.shape_bg_shop_below_corner);
            topLayout.setVisibility(View.GONE);
        } else {
            root.setBackgroundColor(Color.WHITE);
            topLayout.setVisibility(View.GONE);
        }

        // 商家图标
        Glide.with(mContext)
                .load(item.getLogoUrl())
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .placeholder(R.mipmap.ic_serve_default)
                .error(R.mipmap.ic_serve_default)
                .into((ImageView) holder.getView(R.id.iv_shop_logo));

        // 商家名称
        holder.setText(R.id.tv_shop_name, item.getName());
        holder.setText(R.id.tv_price, item.getPrice());
        holder.setText(R.id.tv_distance, item.getDistance() + "m");
        holder.setText(R.id.tv_spec, item.getAddress());

        // 跳转
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (XXPermissions.isHasPermission(mContext, Permission.ACCESS_FINE_LOCATION,
                        Permission.ACCESS_COARSE_LOCATION)) {
                    mContext.startActivity(new Intent(mContext, ACT_ShopDetail.class).putExtra("id", item.getId()));
                } else {
                    XXPermissions.with((Activity) mContext).permission(Permission.ACCESS_COARSE_LOCATION,
                            Permission.ACCESS_FINE_LOCATION
                    ).request(new OnPermission() {
                        @Override
                        public void hasPermission(List<String> granted, boolean isAll) {
                            if (isAll) {
                                mContext.startActivity(new Intent(mContext, ACT_ShopDetail.class).putExtra("id",
                                        item.getId()));
                            }
                        }

                        @Override
                        public void noPermission(List<String> denied, boolean quick) {
                            CommonUtils.makeEventToast(mContext, "权限拒绝了", false);
                        }
                    });
                }
            }
        });

    }
}
